package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.vo.FormDataAddVO;
import com.xbongbong.paas.pojo.vo.FormDataDeleteBatchVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.knowledge.base.pojo.dto.BatchDealTagDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeAddDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeOperationDTO;
import com.xbongbong.pro.knowledge.base.pojo.vo.KnowledgeVO;
import com.xbongbong.saas.service.KnowledgeDataService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 知识条目控制层<p>
 * 代码描述<p>
 * Company: 逍邦网络科技有限公司<p>
 *
 * @author ruansicheng
 * @version v1.0
 * @since 2022-06-23 10:20
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/knowledge")
public class KnowledgeDataController {
    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private KnowledgeDataService knowledgeDataService;

    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addKnowledge(@RequestBody @Valid KnowledgeAddDTO knowledgeAddDTO, BindingResult br) {
        XbbResponse<FormDataAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(knowledgeAddDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
                TransmittableThreadLocalUtil.setValue(knowledgeAddDTO.getSaasMark(), knowledgeAddDTO.getBusinessType(), knowledgeAddDTO.getMenuId(), knowledgeAddDTO.getDistributorMark());

                FormDataAddVO formDataSaveVO= knowledgeDataService.addKnowledge(knowledgeAddDTO);
                response = new XbbResponse<>(formDataSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid KnowledgeOperationDTO knowledgeOperationDTO) {
        XbbResponse<FormDataDeleteBatchVO> response;
        try {
            //重复提交校验
            if (!paasRedisHelper.checkConcurrentLock(knowledgeOperationDTO)) {
                response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                return JSON.toJSONString(response);
            }
            TransmittableThreadLocalUtil.setValue(knowledgeOperationDTO.getSaasMark(), knowledgeOperationDTO.getBusinessType(), knowledgeOperationDTO.getMenuId(), knowledgeOperationDTO.getDistributorMark());
            FormDataDeleteBatchVO formDataDeleteBatchVO=knowledgeDataService.deleteBatch(knowledgeOperationDTO);
            response = new XbbResponse<>(formDataDeleteBatchVO);
        } catch (XbbException e) {
            response = new XbbResponse<>(e);
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);

    }

    @RequestMapping(value = "/updateGet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String essayUpdateGet(@RequestBody @Valid KnowledgeOperationDTO knowledgeOperationDTO) {
        XbbResponse<KnowledgeVO> response;
        try {
            KnowledgeVO knowledgeVO = knowledgeDataService.knowledgeUpdateGet(knowledgeOperationDTO);
            response = new XbbResponse<>(knowledgeVO);
        } catch (XbbException e) {
            response = new XbbResponse<>(e);
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid KnowledgeAddDTO knowledgeAddDTO) {
        XbbResponse<KnowledgeVO> response;
        try {
            //重复提交校验
            if (!paasRedisHelper.checkConcurrentLock(knowledgeAddDTO)) {
                response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                return JSON.toJSONString(response);
            }
            TransmittableThreadLocalUtil.setValue(knowledgeAddDTO.getSaasMark(), knowledgeAddDTO.getBusinessType(), knowledgeAddDTO.getMenuId(), knowledgeAddDTO.getDistributorMark());
            KnowledgeVO knowledgeVO=knowledgeDataService.update(knowledgeAddDTO);
            response = new XbbResponse<>(knowledgeVO);
        } catch (XbbException e) {
            response = new XbbResponse<>(e);
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/setup", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setup(@RequestBody @Valid KnowledgeOperationDTO knowledgeOperationDTO) {
        XbbResponse<FormDataDeleteBatchVO> response;
        try {
            //重复提交校验
            if (!paasRedisHelper.checkConcurrentLock(knowledgeOperationDTO)) {
                response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                return JSON.toJSONString(response);
            }
            TransmittableThreadLocalUtil.setValue(knowledgeOperationDTO.getSaasMark(), knowledgeOperationDTO.getBusinessType(), knowledgeOperationDTO.getMenuId(), knowledgeOperationDTO.getDistributorMark());
            FormDataDeleteBatchVO formDataDeleteBatchVO=knowledgeDataService.setupKnowledge(knowledgeOperationDTO);
            response = new XbbResponse<>(formDataDeleteBatchVO);
        } catch (XbbException e) {
            response = new XbbResponse<>(e);
        }
        return JSON.toJSONString(response);
    }

    /**
     * 批量增加标签
     * @param batchDealTagDTO
     * @return
     */
    @RequestMapping(value = "/batchAddTag", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String batchAddTag(@RequestBody @Valid BatchDealTagDTO batchDealTagDTO) {
        XbbResponse<BaseVO> response;
        try {
            BaseVO baseVO = knowledgeDataService.batchAddTag(batchDealTagDTO);
            response = new XbbResponse<>(baseVO);
        } catch (XbbException e) {
            response = new XbbResponse<>(e);
        }
        return JSON.toJSONString(response);
    }

    /**
     * 批量移除标签
     * @param batchDealTagDTO
     * @return
     */
    @RequestMapping(value = "/batchRemoveTag", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String batchRemoveTag(@RequestBody @Valid BatchDealTagDTO batchDealTagDTO) {
        XbbResponse<BaseVO> response;
        try {
            BaseVO baseVO = knowledgeDataService.batchRemoveTag(batchDealTagDTO);
            response = new XbbResponse<>(baseVO);
        } catch (XbbException e) {
            response = new XbbResponse<>(e);
        }
        return JSON.toJSONString(response);
    }
}
